package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"gopkg.in/ini.v1"
	"log"
	"os"
)
//配置文件相关遍历
var cfg *ini.File
var err error
//初始化数据库
var OriginalDB *gorm.DB
var ScanningDB *gorm.DB

//相关日志文件
var logFile *os.File

func ParameterInitialization(){
	cfg, err = ini.Load("config.ini")
	if err != nil {
		fmt.Printf("Fail to read file: %v", err)
		log.Printf("Fail to read file: %v", err)
		os.Exit(1)
	}
}
func ConnectDatabase() {
	var err1 error
	var Database1 = cfg.Section("Mysql").Key("Username").String() + ":" + cfg.Section("Mysql").Key("Password").String() + "@(127.0.0.1:3306)/" + cfg.Section("Mysql").Key("OriginalDB").String() + "?charset=utf8mb4"
	OriginalDB, err1 = gorm.Open("mysql", Database1) //数据库连接
	if err1 != nil {
		log.Println(err1)
		panic(err1)
	}
	var err2 error
	var Database2 = cfg.Section("Mysql").Key("Username").String() + ":" + cfg.Section("Mysql").Key("Password").String() + "@(127.0.0.1:3306)/" + cfg.Section("Mysql").Key("ScanningDB").String() + "?charset=utf8mb4"
	ScanningDB, err2 = gorm.Open("mysql", Database2) //数据库连接
	if err2 != nil {
		log.Println(err1)
		panic(err1)
	}
}
func ProgramRunningLog() {
	logFile, err := os.OpenFile("./databaseconnecting.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed, err:", err)
		log.Println("open log file failed, err:", err)
		return
	}
	log.SetPrefix("[DataMing]")

	log.SetOutput(logFile)
	log.Println("程序初始化完成")
}


